package org.supda.system.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.WebRequest;
import org.supda.system.util.search.ArraySearch;
import org.supda.system.util.search.DateSearch;
import org.supda.system.util.search.FuzzySearch;

/**
 * 
 * @author supda
 * @Date 2017-1-1 下午4:08:08
 *
 * DataTables插件分页类
 */
public class DataTablePageUtil<T> {  
	  
    private int draw; // 第几次请求  
    private int start = 0;// 起止位置'0'  
    private int length = 10; // 数据长度'10'  
    private List<T> data;  
    private int recordsTotal; // 数据总记录数  
    private int recordsFiltered; // 过滤数  
    private String error;
    
    // 常规查询条件  
	private Map<String, String> condition = new HashMap<String, String>(); 
    
    //扩展查询
    private List<ArraySearch> arraySearchs = new ArrayList<ArraySearch>();
    private List<DateSearch> dateSearchs = new ArrayList<DateSearch>();
    private List<FuzzySearch> fuzzySearchs = new ArrayList<FuzzySearch>();

    public DataTablePageUtil() {  
  
    }  
  
    public DataTablePageUtil(WebRequest request) {  
        if (StringUtils.isNotBlank(request.getParameter("aoData"))) {  
            JSONArray jsonarray = JSONArray.fromObject(request  
                    .getParameter("aoData"));  

            for (int i = 0; i < jsonarray.size(); i++) {  
                JSONObject obj = (JSONObject) jsonarray.get(i);  
                
                if (obj.get("name").equals("sEcho"))  
                    this.setDraw(obj.getInt("value"));  
  
                if (obj.get("name").equals("iDisplayStart"))  
                    this.setStart(obj.getInt("value"));  
  
                if (obj.get("name").equals("iDisplayLength"))  
                    this.setLength(obj.getInt("value"));
                
                if(obj.get("name").toString().startsWith("ext_search_")){
                	condition.put(obj.get("name").toString().substring("ext_search_".length()),obj.getString("value"));
                }
                
                //{ "name": "fuzzySearch", "value": {"value":$("#unknown").val(), "datatype":["email","username","telephone"], "isLike":true, "isAnd":false }},
                if(obj.get("name").equals("fuzzysearch")){
                	FuzzySearch fs = new FuzzySearch();
                	
                	JSONObject jsonObj = obj.getJSONObject("value");
                	
                	fs.setValue(jsonObj.getString("value"));
                	fs.setLike(jsonObj.getBoolean("islike"));
                	fs.setAnd(jsonObj.getBoolean("isand"));
                	
                	JSONArray dataTypeJsonArray	= jsonObj.getJSONArray("datatype");
                	
                	for(int j = 0 ; j < dataTypeJsonArray.size() ; j ++){
                		Object dataType = dataTypeJsonArray.get(j);
                		fs.getDatatype().add(dataType.toString());
                	}
                	
                	if(!StringUtils.isEmpty(fs.getValue()) 
                			&&fs.getDatatype()!=null
                			&&fs.getDatatype().size() > 0)
                	fuzzySearchs.add(fs);
                }
                
                if(obj.get("name").equals("datesearch")){
                	DateSearch ds = new DateSearch();
                	
                	JSONObject jsonObj = obj.getJSONObject("value");
                	
                	ds.setDatemax(jsonObj.getString("datemin"));
                	ds.setDatemin(jsonObj.getString("datemax"));
                	ds.setPattern(jsonObj.getString("pattern"));
                	ds.setDatatype(jsonObj.getString("datatype"));
                	
                	if(!StringUtils.isEmpty(ds.getDatatype()) 
                			&& !StringUtils.isEmpty(ds.getDatemin()) 
                			&& !StringUtils.isEmpty(ds.getDatemax()))
                	dateSearchs.add(ds);
                }
                
                //{ "name": "arraySearch", "value": {"value":$("#unknown").val(), "datatype":"sdas", "isand":true, "splitstr":"," }},
                if(obj.get("name").equals("arraysearch")){
                	ArraySearch as = new ArraySearch();
                	
                	JSONObject jsonObj = obj.getJSONObject("value");
                	
                	as.setAnd(jsonObj.getBoolean("isand"));
                	as.setDatatype(jsonObj.getString("datatype"));
                	as.setSplitStr(jsonObj.getString("splitstr"));
                	as.setValue(jsonObj.getString("value"));
                		
                	arraySearchs.add(as);
                }
            }  
        }  
        if (StringUtils.isNotBlank(request.getParameter("searchData"))  
                && request.getParameter("searchData").startsWith("[")) {  
            JSONArray searchData = JSONArray.fromObject(request  
                    .getParameter("searchData"));  
            if (searchData != null && searchData.size() > 0) {  
                for (int i = 0; i < searchData.size(); i++) {  
                    JSONObject jsonObject = searchData.getJSONObject(i);  
                    if (StringUtils.isNotBlank(jsonObject.getString("value"))) {  
                        this.condition.put(jsonObject.getString("name"),  
                                jsonObject.getString("value"));  
                    }  
                }  
            }  
        }  
    }  
  
    public int getDraw() {  
        return draw;  
    }  
  
    public void setDraw(int draw) {  
        this.draw = draw;  
    }  
  
    public int getStart() {  
        return start;  
    }  
  
    public void setStart(int start) {  
        this.start = start;  
    }  
  
    public int getLength() {  
        return length;  
    }  
  
    public void setLength(int length) {  
        this.length = length;  
    }  
  
    public List<T> getData() {  
        return data;  
    }  
  
    public void setData(List<T> data) {  
        this.data = data;  
    }  
  
    public Map<String, String> getCondition() {  
        return condition;  
    }  
  
    public void setCondition(Map<String, String> condition) {  
        this.condition = condition;  
    }  
  
    public int getRecordsTotal() {  
        return recordsTotal;  
    }  
  
    public void setRecordsTotal(int recordsTotal) {  
        this.recordsTotal = recordsTotal;  
    }  
  
    public int getRecordsFiltered() {  
        return recordsFiltered;  
    }  
  
    public void setRecordsFiltered(int recordsFiltered) {  
        this.recordsFiltered = recordsFiltered;  
    }

	@Override
	public String toString() {
		return "DataTablePageUtil [draw=" + draw + ", start=" + start
				+ ", length=" + length + ", data=" + data + ", recordsTotal="
				+ recordsTotal + ", recordsFiltered=" + recordsFiltered
				+ ", condition=" + condition + "]";
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public List<ArraySearch> getArraySearchs() {
		return arraySearchs;
	}

	public void setArraySearchs(List<ArraySearch> arraySearchs) {
		this.arraySearchs = arraySearchs;
	}

	public List<DateSearch> getDateSearchs() {
		return dateSearchs;
	}

	public void setDateSearchs(List<DateSearch> dateSearchs) {
		this.dateSearchs = dateSearchs;
	}

	public List<FuzzySearch> getFuzzySearchs() {
		return fuzzySearchs;
	}

	public void setFuzzySearchs(List<FuzzySearch> fuzzySearchs) {
		this.fuzzySearchs = fuzzySearchs;
	}  
	
   
}  